package com.byq.mm.smartlib.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.byq.mm.smartlib.R;
import com.byq.mm.smartlib.component.TreeView;
import com.byq.mm.smartlib.component.TreeView.TreeCallBack;
import com.byq.mm.smartlib.component.TreeView.TreeElement;
import com.byq.mm.abstractx.cluster.server.DeviceServer;
import com.byq.mm.abstractx.cluster.server.ServerDeviceManager;
import com.byq.mm.abstractx.device.Cluster;
import com.byq.mm.abstractx.device.Device;

public class ServerManagerActivity extends AbstractSmartActivity {
	ServerDeviceManager server;
	ListView clusterView;
	Button back;
//	ListView clientsView;
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.setContentView(R.layout.server_manager);
		server = application.getAndroidSmartEngine().getCoreEngine().getServerDeviceManager();
		TreeView view = new TreeView(this, cb);
	    view.setPadding(10, 10,10,10);	
		LinearLayout container = (LinearLayout)this.findViewById(R.id.listContainer);
		back = (Button)this.findViewById(R.id.back_server_manager);

		LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(

		LinearLayout.LayoutParams.MATCH_PARENT,  

		LinearLayout.LayoutParams.MATCH_PARENT  

		); 

		view.setBackgroundResource(R.drawable.list_bg);

		container.addView(view,p);
//		TextView name = (TextView) this.findViewById(R.id.name);
//	    name.setText(server.getLocalServerDevice().getTransportId());
		back.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View view) {
				ServerManagerActivity.this.finish();
			}
		});
	}
	TreeCallBack cb = new TreeCallBack(){
		@Override
		public List<TreeElement> createElements() {
			return createAll();
		}
		List<TreeElement> createPhone() {
			Device d = server.getLocalServerDevice();
			DeviceServer ds = server.getDeviceServer(d.getId());
			TreeElement dt = TreeView.createElement(d.getName(),ds);
			dt.setExpanded(true);
			for(Cluster c : ds.getDevice().getSupportedClusters()){
				String name = c.getAlias();
				if(name == null){
					
					 int rid = getResources().getIdentifier(c.getId(),  "string",getPackageName());
					 if(rid == 0){
						 Log.e("resource", "no string for " + c.getId());
						 name = c.getId();
					 }else	 name = getResources().getString(rid);
				}
				TreeElement ct = TreeView.createElement(name, ds.getClusterServer(c.getId()), dt);
				ct.setExpanded(true);
//				ClusterServer cs = ds.getClusterServer(c.getId());
//				for(ClientEntry ce : cs.getAccessController().getClientEntrys()){
//					TreeView.createElement(ce.clientTransport,ce, ct);
//				}
				
				
			}
			List<TreeElement> rst = new ArrayList<TreeElement>();
			rst.add(dt);
			return rst;
		}
		public List<TreeElement> createAll() {
			TreeElement root = TreeView.createElement(getResources().getString(R.string.all_devices), server);
			root.setExpanded(true);
			Device d = server.getLocalServerDevice();
			DeviceServer ds = server.getDeviceServer(d.getId());
			TreeElement dt = TreeView.createElement(d.getName(),ds , root);

			item(ds, dt);
			List<TreeElement> rst = new ArrayList<TreeElement>();
			rst.add(root);
			return rst;
		}
		void item(DeviceServer d, TreeElement parent){
			parent.setExpanded(true);
			

			TreeElement cs = TreeView.createElement(getResources().getString(R.string.supported_clusters), parent);
			cs.setExpanded(true);
			for(Cluster c : d.getDevice().getSupportedClusters()){
				String name = c.getAlias();
				if(name == null){
					
					 int rid = getResources().getIdentifier(c.getId(),  "string",getPackageName());
					 name = getResources().getString(rid);
				}
				TreeView.createElement(name, d.getClusterServer(c.getId()), cs).setExpanded(true);
				
			}
			
			if(d.getDevice().getSubdevices().isEmpty())
				return;
			TreeElement subd = TreeView.createElement(getResources().getString(R.string.sub_devices), parent);
			subd.setExpanded(true);
			for(Device c : d.getDevice().getSubdevices()){
				DeviceServer ds = server.getDeviceServer(c.getId());
				TreeElement ct = TreeView.createElement(c.getName(), ds, subd);
				item(ds, ct);
			}
			
		}

		@Override
		public void elementSelected(TreeElement element) {
			element.setSelected(true);
			
		}

		@Override
		public Boolean equalByTag(Object tag0, Object tag1) {
			// TODO Auto-generated method stub
			return true;
		}
		
	};
	

}
